
do "...\First.do"

********************************************************************************
** Clean data for clinics 

* In this file I clean data for PCP clinics. 
* The raw dataset includes information about all people working in private clinics,
* I will keep information about doctors and primary care clinics.


** The finished dataset contains ydernr-pnr match speciality and year

********************************************************************************
** T_yder_1 contains information from 1995-2014
********************************************************************************
clear 
use "$raw\T_yder_1.dta"

keep if c_hspec=="80" // Keep only PCPs

keep ydernr pnr v_aar
rename v_aar year
duplicates drop

bys ydernr year: g N_doctors=_N

g solo=(N_doctors==1)

save "$work\T_yder_1.dta", replace


keep if year==2014
save "$work\GP_2014", replace


********************************************************************************
* Clean data from 2015-2018
** The raw dataset does not include ydernr x pnr in each year, but the clinics 
** are only included if there are changes to the composition. Therefore I will
** use information about start and end dates to fill out the pnrs such that the 
** finished dataset includes the doctor in the clinic in each year.

** For clinics that does not appear in the dataset "t_yder_person", but I observe
** them in 2014 in "T_yder_1" they operate through out the 2014-2018
** period without changes.
********************************************************************************

clear
use "$raw\t_yder_person.dta"

merge m:1 pnr using "$work\doctor_udd.dta"
drop if _merge==1 & pnr!=""
drop if _merge==2
drop _merge

keep if speciale_i_almen_medicin=="J" // PCPs
keep ydernr pnr afgangsdato_person doctor tilgangsdato_person


duplicates drop
g help=!missing(afgangsdato)
bys pnr ydernr: egen nmis=max(help)

cap drop y_2*
g y_2014=.
g y_2015=.
g y_2016=.
g y_2017=.
g y_2018=.
g y_2019=.


replace y_2014=1 if year(tilgangsdato)<=2014 
replace y_2015=1 if year(tilgangsdato)<=2015 & ((year(afgangsdato)>=2015) | afgangsdato==.) 
replace y_2016=1 if year(tilgangsdato)<=2016 & ((year(afgangsdato)>=2016) | afgangsdato==.) 
replace y_2017=1 if year(tilgangsdato)<=2017 & ((year(afgangsdato)>=2017) | afgangsdato==.) 
replace y_2018=1 if year(tilgangsdato)<=2018 & ((year(afgangsdato)>=2018) | afgangsdato==.) 
replace y_2019=1 if year(tilgangsdato)<=2019 & ((year(afgangsdato)>=2019) | afgangsdato==.)

keep ydernr pnr y_2*
duplicates drop 
bys pnr ydernr: g N=_N
sort pnr ydernr y_2014 y_2015 y_2016 y_2017 y_2018 y_2019

egen rowsum=rownonmiss(y_2014 y_2015 y_2016 y_2017 y_2018 y_2019)
bys pnr ydernr: egen max=max(rowsum)
keep if max==rowsum
drop N rowsum max

bys pnr ydernr: g N=_N
drop N

expand 6
sort pnr ydernr

bys pnr ydernr: g n=_n
g year=n+2013
drop n  
tab year

forvalues v=2014/2019{
	drop if y_`v'==. & year==`v'
}

drop y_2014-y_2019

bys ydernr year: g N_doctors=_N
g solo=(N_doctors==1)
drop if year>2019



* Doctors from 2015-2018
save "$work\doctor_1518.dta", replace

*************************************************

clear 
use "$work\doctor_1518.dta"

keep ydernr
duplicates drop

* Append the doctors with no changes across the 2014-2018 period
merge 1:m ydernr using "$work\GP_2014"
keep if _merge==2
drop _merge

cap drop y_2*
g y_2014=.
g y_2015=.
g y_2016=.
g y_2017=.
g y_2018=.
g y_2019=.

replace y_2014=1  
replace y_2015=1 
replace y_2016=1 
replace y_2017=1 
replace y_2018=1 
replace y_2019=1 

expand 6
sort pnr ydernr

drop year
bys pnr ydernr: g n=_n
g year=n+2013
drop n  
tab year

drop y_2014-y_2019


save "$work\doctor_1518_append.dta", replace


*************************************************
* Find speciality in 2014-2018
*************************************************


clear
use "$raw\Yder_2"

keep ydernr  hovedspeciale_kode

keep if hovedspeciale_kode=="80" // PCPs
keep ydernr
duplicates drop

save "$work\yder2.dta", replace

*************************************************
* Merge ydernr and pnr
*************************************************

clear 
use "$work\doctor_1518.dta"

drop if year==2014

merge m:1 ydernr using "$work\yder2.dta" // Keeping PCPs
keep if _merge==3
drop _merge

append using "$work\T_yder_1.dta" // 1995-2014 data
append using "$work\doctor_1518_append.dta" // Unchanged clinics in 2015-2018


order ydernr pnr year
drop if missing(pnr)
drop if trim(pnr)=="."

sort ydernr year
duplicates drop

* Keeps only indidivuals that are educated within medicin
merge m:1 pnr using "$work\doctor_udd.dta"
keep if _merge==3
drop _merge

drop if year<year_doctor

bys ydernr year: g N_doctors2=_N
tab year

save "$work\Yder_9518_wide.dta", replace


